<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-input placeholder="请输入内容" v-model="searchValue" class="input-with-select" clearable @clear="reGetInfo">
        <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
      </el-input>
      <el-button type="primary" @click="addProduct">添加商品</el-button>

      <!-- 表格区域 -->
      <el-table :data="paramsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" width="40"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120"> </el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="120"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editPro(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="dele(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[20, 30, 40, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchValue: '',
      paramsList: [],
      total: 0,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 20,
      },
    }
  },
  created() {
    this.getparamsList()
  },
  methods: {
    //获取所有商品列表
    async getparamsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取列表失败')
      this.$message.success('获取商品列表成功')
      console.log(res)
      this.paramsList = res.data.goods
      this.total = res.data.total
    },

    //当分页按钮的当前页发生改变的时候
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getparamsList()
    },

    //当当前页发生变化的时候, 重新发送请求,获取数据
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getparamsList()
    },

    //根据关键字搜索对应的内容
    search() {
      this.queryInfo.query = this.searchValue.trim()
      console.log(this.queryInfo.query)
      this.getparamsList()
    },

    //当输入框的内容清空以后, 就重新获取列表数据
    reGetInfo() {
      this.queryInfo.query = ''
      this.getparamsList()
    },

    //点击增加商品, 跳转到对应页面
    addProduct() {
      this.$router.push('/goods/add')
    },

    async dele(role) {
      console.log(role)
      let confirm = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        type: 'warning',
      }).catch(err => err)
      console.log(confirm);
      if (confirm == 'confirm') {
        const { data: res } = await this.$http.delete(`goods/${role.goods_id}`)
          console.log(res, role.goods_id);
        if (res.meta.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除商品成功')
        this.getparamsList()
      }
    },
  },
}
</script>

<style scoped lang="less">
.el-input {
  width: 400px;
  margin-right: 20px;
}
</style>
